﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BlawkayEngine
{
    public class LineSegment
    {

        #region Constructors

        public LineSegment(float Ax, float Ay, float Bx, float By)
            : this(new Vector2(Ax, Ay), new Vector2(Bx, By))
        { }

        public LineSegment(Vector2 pointA, Vector2 pointB)
        {
            _PointA = pointA;
            _PointB = pointB;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Changes the location of the starting position.
        /// </summary>
        /// <param name="a">New Location</param>
        public void SetA(Vector2 a)
        {
            _PointA = a;
        }

        /// <summary>
        /// Changes the location of the ending position.
        /// </summary>
        /// <param name="b">New Location</param>
        public void SetB(Vector2 b)
        {
            _PointB = b;
        }

        /// <summary>
        /// Swaps the Endpoints so that A is B and B is A.
        /// </summary>
        public void SwapPoints()
        {
            LineSegment oSwapped = this.BtoA;

            this.SetA(oSwapped.A);
            this.SetB(oSwapped.B);
        }

        #endregion

        #region Properties

        public Vector2 A
        {
            get { return _PointA; }
        }

        public Vector2 B
        {
            get { return _PointB; }
        }

        public Vector2 MidPoint
        {
            get
            {
                return (A + B) / 2;
            }
        }

        public float Distance
        {
            get
            {
                return Vector2.Distance(A, B);
            }
        }

        public Vector2 Direction
        {
            get
            {
                Vector2 direction = (B - A);

                if (direction != Vector2.Zero)
                    direction.Normalize();

                return direction;
            }
        }

        public float Angle
        {
            get
            {
                if (Direction != Vector2.Zero)
                    return VectorHelper.VectorToAngle(Direction);
                else
                    return 0;
            }
        }

        public LineSegment BtoA
        {
            get
            {
                return new LineSegment(B, A);
            }
        }

        #endregion

        #region Fields

        Vector2 _PointA;
        Vector2 _PointB;

        #endregion

    }
}
